package nix.demo2.service;

import nix.demo2.dao.DaoFactory;
import nix.demo2.dao.UserDao;
import nix.demo2.domain.User;

public class UserService {

	private UserDao userDao = DaoFactory.getUserDao();

	public void register(User user) throws UserException {

		User _user = userDao.findByUsername(user.getUsername());
		if (_user != null) {
			throw new UserException("用户名：" + user.getUsername() + "已被注册了！");
		}
		userDao.add(user);
	}

	public User login(User form) throws UserException {

		User user = userDao.findByUsername(form.getUsername());
		if (user == null) {
			throw new UserException("用户名：" + form.getUsername() + "不存在！");
		}
		if (!user.getPassword().equals(form.getPassword())) {
			throw new UserException("密码不正确！");
		}
		return user;
	}
}
